// Tudou.js for PSPTube
// Made By JK108

function Tudou_CheckURL( url, option )
{
	if (url.match("http://www.tudou.com") == "http://www.tudou.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Tudou URL
*/
function Tudou_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/var iid \= (.*)/);
	var contents = GetContents("http://v2.tudou.com/v2/cdn?safekey=IAlsoNeverKnow&id=" + video_url[1]);
	if(contents == null) { return null; /* failed to get page */ }
	var pos = contents.indexOf("\">");
	contents = contents.substring(pos + 2, contents.length);
	var video_url = contents.match(/\"\>(.*?)\<\/f\>/);
	var video_url = video_url[1];
	return video_url;
}

function Tudou_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 16;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://so.tudou.com/isearch/" + keyword.replace(/ /g, "%20") + "/" );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://so.tudou.com/isearch/" + keyword.replace(/ /g, "%20") + "/cid__time__sort_score_display_album_page_" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	//if(contents.match("</a> <em>") == "</a> <em>"){ result.total = 0; return result; }
	//var total = contents.match(/\<\/a\>\<\/em\>(.*?)\<\/em\>/);
	//var pos = total[1].indexOf("<em>");
	//var total = parseInt(total[1].substring(pos + 4, total[1].length).replace(/,/g, ""));
	result.total = 10000;//total;


	while(1){
	var pos = contents.indexOf("<div class=\"pack pack_video_card\">");

	if(pos != -1){
		contents = contents.substring(pos + 34, contents.length);
		//Get URL
		var url = contents.match(/\" href\=\"(.*?)\"/);
		var url = url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\" src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/\" title\=\"(.*?)\"/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/\<li\>(.*?)\/li\>/);
		var length = length[1].match(/\: (.*?)\</);
		var length = TimestringToSeconds(length[1]);
		//Description
		var description = contents.match(/\: \<a title\=\"(.*?)\"/);
		if(description[1] == null || description[1] == ""){description[1] = "Unknown";}
		var description = ImproveText("Posted By: " + description[1]);
		//Views
		pos = contents.indexOf("<li>");
		var views = contents.substring(pos + 4, contents.length);
		pos = views.indexOf("<li>");
		var views = views.substring(pos + 4, views.length);
		var views = views.match(/\<li\>(.*?)\/li\>/);
		var views = views[1].match(/\: (.*?)\</);
		var views = parseInt(views[1]);
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Tudou = new Object();
Tudou.Name          = "Tudou";		/* Service name */
Tudou.Description   = "Tudou";		/* Service description */
Tudou.SearchDesc    = "Tudou";		/* Service search screen description */
Tudou.SearchOSKMode = 1;		/* OSK mode 1: English only  2: Japanese */
Tudou.CheckURL      = Tudou_CheckURL;	/* CheckURL function */
Tudou.GetURL        = Tudou_GetURL;	/* GetURL function */
Tudou.Search        = Tudou_Search;	/* Search function */

// add Tudou to site list
SiteList.push( Tudou );
